package cn.funeralobjects.upms.controller;

import cn.funeralobjects.common.service.EnableService;
import cn.funeralobjects.common.service.EntityService;
import cn.funeralobjects.common.service.IdService;
import cn.funeralobjects.common.web.controller.RestEnableController;
import cn.funeralobjects.common.web.controller.RestEntityController;
import cn.funeralobjects.common.web.controller.converter.VoMapper;
import cn.funeralobjects.upm.web.model.VApi;
import cn.funeralobjects.upms.api.ApiApi;
import cn.funeralobjects.upms.controller.converter.ApiMapper;
import cn.funeralobjects.upms.entity.EApi;
import cn.funeralobjects.upms.enums.HttpMethod;
import cn.funeralobjects.upms.enums.Protocol;
import cn.funeralobjects.upms.model.Api;
import cn.funeralobjects.upms.service.ApiService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * Api实体外部访问接口
 *
 * @author FuneralObjects
 * Create date: 2020/4/28 2:15 PM
 */
@RestController
@RequestMapping(name = "api", path = "/api")
public class ApiController implements ApiApi, RestEntityController<VApi, EApi, Integer>, RestEnableController<VApi, EApi, Integer> {

    @Resource
    private ApiMapper apiMapper;

    @Resource
    private ApiService apiService;

    @PostMapping("/{scope}/http")
    @Override
    public Mono<VApi> addHttpApi(@PathVariable String scope,
                                 @RequestParam HttpMethod method,
                                 @RequestParam String url,
                                 @RequestParam(required = false) String name) {
        return Mono.just(new VApi().setMethod(method).setScope(scope).setProtocol(Protocol.HTTP).setName(name).setUrl(url))
                .map(api -> apiService.addApi(api.getScope(), api.getName(), api.getProtocol(), api.getMethod(), api.getUrl()))
                .map(apiMapper::toVo);
    }

    @PostMapping("/batch")
    @Override
    public Mono<List<VApi>> addOrUpdateInBatch(@RequestBody @NotEmpty List<Api> apis) {
        return Mono.just(apis)
                .map(apiService::addOrUpdateAll)
                .map(apiMapper::toVo);
    }

    @PostMapping("/{scope}/ws")
    @Override
    public Mono<VApi> addWsApi(@PathVariable String scope,
                               @RequestParam String url,
                               @RequestParam(required = false) String name) {
        return Mono.just(new VApi().setMethod(HttpMethod.NONE).setScope(scope).setProtocol(Protocol.WS).setName(name).setUrl(url))
                .map(api -> apiService.addApi(api.getScope(), api.getName(), api.getProtocol(), api.getMethod(), api.getUrl()))
                .map(apiMapper::toVo);
    }

    @GetMapping("/all")
    @Override
    public Mono<List<VApi>> findAll() {
        return Mono.just(true)
                .map(t -> apiService.findAll())
                .map(apiMapper::toVo);
    }

    @Override
    public EnableService<EApi, Integer> enableService() {
        return apiService;
    }

    @Override
    public EntityService<EApi, Integer> entityService() {
        return apiService;
    }

    @Override
    public IdService<Integer> idService() {
        return apiService;
    }

    @Override
    public VoMapper<EApi, VApi> voMapper() {
        return apiMapper;
    }
}
